#ifndef __PROCESS_POOL_HPP__
#define __PROCESS_POOL_HPP__

#include <iostream>
#include <vector>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "Task.hpp"
using namespace std;

class Channel
{
public:
    Channel(int fd, pid_t id)
        : _wfd(fd), _fid(id)
    {
        _name = "Channel-" + to_string(_fid) + '-' + to_string(_wfd);
    }

    ~Channel()
    {
    }

    string Name()
    {
        return _name;
    }

    int Fd()
    {
        return _wfd;
    }

    pid_t id()
    {
        return _fid;
    }

    void Send(int code)
    {
        // int n = write(_fid, &code, sizeof(code));
        int n = write(_wfd, &code, sizeof(code));
        (void)n;
    }

    void Close()
    {
        close(_wfd);
    }

    void Wait()
    {
        pid_t pid = waitpid(_fid, nullptr, 0);
        (void)pid;
    }

private:
    int _wfd;     // 文件标识符
    string _name; // 进程名字
    pid_t _fid;   // 子进程pid
};

class ChannelManager
{
public:
    ChannelManager()
        : _next(0)
    {
    }

    void Insert(int fd, pid_t pid)
    {
        _channels.emplace_back(fd, pid);
    }

    void ChannelPrint()
    {
        for (auto &c : _channels)
        {
            cout << "进程名字：" << c.Name() << endl;
        }
    }

    void Stop()
    {
        for (auto &c : _channels)
        {
            cout << "进程关闭：" << c.Name() << endl;
            c.Close();
        }
    }

    void Wait()
    {
        for (auto &c : _channels)
        {
            cout << "进程回收：" << c.Name() << endl;
            c.Wait();
        }
    }

    Channel &Select()
    {
        auto &c = _channels[_next++];
        _next %= _channels.size();
        return c;
    }

    ~ChannelManager()
    {
    }

private:
    vector<Channel> _channels;
    int _next;
};

class ProcessPooL
{
public:
    ProcessPooL(int num)
        : _num(num)
    {
        _tm.Register(PrintLog);
        _tm.Register(Upload);
        _tm.Register(DownLoad);
    }

    ~ProcessPooL()
    {
    }

    void Work(int rfd)
    {
        while (true)
        {
            int code = 0;
            //cout << "子进程read开始"  <<endl;
            ssize_t n = read(rfd, &code, sizeof(code));
            //cout << "子进程read结束"  <<endl;
            if (n > 0)
            {
                if (n != sizeof(code))
                {
                    continue;
                }
                cout << "子进程[" << getpid() << "]:接收到一个任务码" << endl;
                sleep(1);
                _tm.Execute(code);
            }
            else if (n == 0)
            {
                cout << "子进程[" << getpid() << "]退出" << endl;
                sleep(1);
                break;
            }
            else
            {
                cout << "读取错误" << endl;
                break;
            }
        }
    }

    bool Start()
    {
        for (int i = 0; i < _num; i++)
        {
            int fd[2] = {0};
            int n = pipe(fd);
            if (n < 0)
            {
                cout << "创建管道失败" << endl;
                return false;
            }

            pid_t id = fork();
            if (id < 0)
            {
                cout << "创建进程失败" << endl;
                return false;
            }
            else if (id == 0) // 子进程 读
            {
                close(fd[1]);
                Work(fd[0]);
                close(fd[0]);
                exit(0);
            }
            else // 父进程 写
            {
                close(fd[0]);
                _cm.Insert(fd[1], id);
                // close(fd[1]);
            }
        }
        return true;
    }

    void Run()
    {
        int task_code = _tm.Code();
        auto &c = _cm.Select();
        cout << "选择了一个进程:" << c.Name() << endl;
        c.Send(task_code);
        cout << "发送一个任务码:" << task_code << endl;
        sleep(1);
    }

    void StopProcess()
    {
        _cm.Stop();
        _cm.Wait();
    }

    void PrintProcess()
    {
        _cm.ChannelPrint();
    }

private:
    ChannelManager _cm;
    TaskManager _tm;
    int _num; // 子进程数量
};

#endif